package com.linkoog.devtools.code_generator.database.sqlparser.parser;

import com.linkoog.devtools.code_generator.database.domain.ColumnInfo;
import com.linkoog.devtools.code_generator.database.domain.DbType;
import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.setting.persistent.CodeGenDevToolsPersistent;
import com.linkoog.devtools.code_generator.util.StringUtil;
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
import net.sf.jsqlparser.statement.create.table.CreateTable;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class MysqlSqlParser extends AbstractSqlParser implements SqlParser {

    Map<String,String> mapping = CodeGenDevToolsPersistent.getInstance().getTypeMappings(DbType.MYSQL);

    @Override
    protected void parseTableInfo(CreateTable createTable, TableInfo tableInfo){
        String tableName = createTable.getTable().getName().replaceAll("`","");
        tableInfo.setTableName(tableName);

        String tableComment = getTableComment(createTable);
        tableInfo.setTableComment(tableComment);

        tableInfo.setJavaEntityName(StringUtil.textToCamelCase(tableInfo.getTableName(),true));
    }

    @Override
    protected void parseColumnInfo(ColumnDefinition columnDefinition, ColumnInfo columnInfo) {
        String columnName = columnDefinition.getColumnName().replaceAll("`","");
        columnInfo.setColumnName(columnName);
        columnInfo.setJavaFieldName(StringUtil.textToCamelCase(columnInfo.getColumnName(),false));

        ColDataType colDataType = columnDefinition.getColDataType();
        columnInfo.setColumnType(colDataType.toString());
        columnInfo.setColumnDataType(colDataType.getDataType().toLowerCase());

        String temp = mapping.get(columnInfo.getColumnDataType());
        columnInfo.setJavaFieldType(StringUtils.isNotBlank(temp)? temp : "Object");


        List<String> columnSpecs = columnDefinition.getColumnSpecs();
        if (columnSpecs != null){
            if (columnSpecs.size() >= 1){
                String str0 = columnSpecs.get(0);
                if ("NULL".equalsIgnoreCase(str0)){
                    columnInfo.setColumnNullable(true);
                }

                if (columnSpecs.size() >= 2){
                    String str1 = columnSpecs.get(1);
                    if ("NOT".equalsIgnoreCase(str0) && "NULL".equalsIgnoreCase(str1)){
                        columnInfo.setColumnNullable(false);
                    }
                }
            }


            for (int i = 0; i < columnSpecs.size(); i++) {
                String columnSpec = columnSpecs.get(i);
                int valIdx = i + 1;
                if ("COMMENT".equalsIgnoreCase(columnSpec) && valIdx < columnSpecs.size()){
                    String columnComment = columnSpecs.get(valIdx);
                    columnInfo.setColumnComment(columnComment.replaceAll("'",""));
                }

                if ("DEFAULT".equalsIgnoreCase(columnSpec) && valIdx < columnSpecs.size()){
                    String defaultVal = columnSpecs.get(valIdx);
                    if (!"NULL".equalsIgnoreCase(defaultVal)){
                        columnInfo.setColumnDefault(defaultVal.replaceAll("'",""));
                    }
                }
            }
        }
    }

    private String getTableComment(CreateTable createTable) {
        List<String> tableOptionsStrings = createTable.getTableOptionsStrings();
        for (int i = 0; i < tableOptionsStrings.size(); i++) {
            String temp = tableOptionsStrings.get(i);
            int valIdx = i + 2;
            if ("COMMENT".equalsIgnoreCase(temp) && valIdx < tableOptionsStrings.size()){
                String tableComment = tableOptionsStrings.get(valIdx);
                return StringUtil.isBlank(tableComment)? "" :  tableComment.replaceAll("'","");
            }
        }
        return "";
    }


}
